﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using SevenInvaders.common;

namespace SevenInvaders.LEVEL_PEDRO
{
    class Level_Pedro
    {
        PedroRenatoPlayer pedroRenatoPlayer1, pedroRenatoPlayer2;

        Texture2D naveReta, naveEsquerda, naveDireita, inimigoTexture,texturaTiro;

        float addInimigo;

        Random randomPosition;

        int numeroDaPosicaoX, numeroDaPosicaoY;
        
        Power powerup;

        Boolean perdeu = false;

        Vida v1P1, v2P1, v3P1;

        Vida v1P2, v2P2, v3P2;

        Texture2D texVida;

        TimeSpan tempoDeJogo;

        public Level_Pedro(ContentManager content, GameWindow window)
        {            
            naveReta = content.Load<Texture2D>("LEVEL_PEDRO/NaveReta");
            naveEsquerda = content.Load<Texture2D>("LEVEL_PEDRO/NaveEsquerda");
            naveDireita = content.Load<Texture2D>("LEVEL_PEDRO/NaveDireita");
            inimigoTexture = content.Load<Texture2D>("LEVEL_PEDRO/asteroide1");
            texturaTiro = content.Load<Texture2D>("LEVEL_PEDRO/texturaTiro");
            texVida = content.Load<Texture2D>("LEVEL_PEDRO/vida");

            v1P1 = new Vida(texVida, 1);
            v2P1 = new Vida(texVida, 2);
            v3P1 = new Vida(texVida, 3);

            v1P2 = new Vida(texVida, 11);
            v2P2 = new Vida(texVida, 22);
            v3P2 = new Vida(texVida, 33);

            pedroRenatoPlayer1 = new PedroRenatoPlayer(naveReta, new Vector2( 300, 500), 1, window, content, texturaTiro);

            pedroRenatoPlayer2 = new PedroRenatoPlayer(naveReta, new Vector2(400, 500), 2, window, content, texturaTiro);

            randomPosition = new Random();

            //inimigo = new PedroRenatoEnemy(inimigoTexture, new Vector2(200, -10), window, content);

            
            for (int i = 0; i < 5; i++)
            {
                PedroRenatoEnemy.list.Add(new PedroRenatoEnemy(inimigoTexture, new Vector2(50 * i, -15 * i), window, content));

            }

            // Dando o BUG --------------------------------------------------------------------------------------
            //inimigo = new PedroRenatoEnemy(inimigoTexture, Vector2.Zero, window, content);

            //PedroRenatoEnemy.list.Add(inimigo);

            //powerup = new Power(content.Load<Texture2D>("LEVEL_PEDRO/PowerUp"), new Vector2(300, 0), 1, 15);

            //Power.list.Add(powerup);
            // Dando o BUG --------------------------------------------------------------------------------------
            
        }

        public void Update(GameTime gameTime, KeyboardState teclado, KeyboardState teclado_anterior)
        {
            
            // PLAYER 1 ---------------------------------------------------------------

            pedroRenatoPlayer1.Update(gameTime, teclado, teclado_anterior);

            if (pedroRenatoPlayer1.vidaPlayer1 >= 1)
            {
                v1P1.Update(gameTime);
            }

             if (pedroRenatoPlayer1.vidaPlayer1 >= 2)
            {
                v2P1.Update(gameTime);
            }

            if (pedroRenatoPlayer1.vidaPlayer1 == 3)
            {
                v3P1.Update(gameTime);
            }

            // PLAYER 2 ------------------------------------------------------------------

            pedroRenatoPlayer2.Update(gameTime, teclado, teclado_anterior);

            if (pedroRenatoPlayer2.vidaPlayer2 >= 1)
            {
                v1P1.Update(gameTime);
            }

            if (pedroRenatoPlayer2.vidaPlayer2 >= 2)
            {
                v2P1.Update(gameTime);
            }

            if (pedroRenatoPlayer2.vidaPlayer2 == 3)
            {
                v3P1.Update(gameTime);
            }


            // INIMIGO -----------------------------------------------------------------------
            //if (pedroRenatoPlayer2.vidaPlayer2 > 0)
            //{
            //    inimigo.Update(gameTime);
            //}
            //if (inimigo.PositionY >= 600)
            //{
            //    inimigo.PositionY = 0 - inimigo.Texture.Height;
            //    inimigo.PositionX = inimigo.randomNumber;
            //    pedroRenatoPlayer1.vidaPlayer1 -= 1;
            //    pedroRenatoPlayer2.vidaPlayer2 -= 1;
            //}

            for (int i = 0; i < PedroRenatoEnemy.list.Count; i++)
            {
                PedroRenatoEnemy.list[i].Update(gameTime);
            }

            // GERAL ------------------------------------------------------------  //

            numeroDaPosicaoX = randomPosition.Next(0, 801);
            numeroDaPosicaoY = randomPosition.Next(0, 601);

            //remover os tiros que não estão mais ativos/visíveis
            for (int i = 0; i < Shoot.list.Count; i++)
            {
                if (
                    Shoot.list[i].Visible == false
                    || Shoot.list[i].PositionY < -10
                    )
                {
                    Shoot.list.RemoveAt(i);
                }
            }

            if (pedroRenatoPlayer1.vidaPlayer1 > 0 || pedroRenatoPlayer2.vidaPlayer2 > 0)
            {
                tempoDeJogo += gameTime.ElapsedGameTime;

                for (int i = 0; i < Shoot.list.Count; i++)
                {
                    Shoot.list[i].Update(gameTime);
                                      
                }
            }

            for (int i = 0; i < Shoot.list.Count; i++)
            {
                for (int j = 0; j < PedroRenatoEnemy.list.Count; j++)
                {
                    if (Shoot.list[i].Collision.Intersects(PedroRenatoEnemy.list[j].Collision)
                        && Shoot.list[i].Visible)
                    {
                        PedroRenatoEnemy.list[j].vida -= 1;
                        pedroRenatoPlayer1.pontos += 25;
                        Shoot.list[i].Visible = false;
                    }
                }
            }
            

            // ----------------------------------------------------------- GERAL //
           

            // Dando o BUG -------------------------------------------------------------
            /*for (int j = 0; j < PedroRenatoEnemy.list.Count; j++)
            {
                PedroRenatoEnemy.list[j].Update(gameTime);
            }

            for (int k = 0; k < Power.list.Count; k++)
            {
                Power.list[k].Update(gameTime);
            }*/
            // Dando o BUG -------------------------------------------------------------
            
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            // PLAYER 1 ---------------------------------------------------------
                                                                                        
            pedroRenatoPlayer1.Draw(gameTime, spriteBatch);

            if (pedroRenatoPlayer1.vidaPlayer1 >= 1)
            {
                v1P1.Draw(gameTime, spriteBatch);
            }

            if (pedroRenatoPlayer1.vidaPlayer1 >= 2)
            {
                v2P1.Draw(gameTime, spriteBatch);
            }

            if (pedroRenatoPlayer1.vidaPlayer1 == 3)
            {
                v3P1.Draw(gameTime, spriteBatch);
            }

            // PLAYER 2 ---------------------------------------------------------

            pedroRenatoPlayer2.Draw(gameTime, spriteBatch);

            if (pedroRenatoPlayer2.vidaPlayer2 >= 1)
            {
                v1P2.Draw(gameTime, spriteBatch);
            }

            if (pedroRenatoPlayer2.vidaPlayer2 >= 2)
            {
                v2P2.Draw(gameTime, spriteBatch);
            }

            if (pedroRenatoPlayer2.vidaPlayer2 == 3)
            {
                v3P2.Draw(gameTime, spriteBatch);
            }


            // INIMIGO ---------------------------------------------------------
            //inimigo.Draw(gameTime, spriteBatch);

            for (int i = 0; i < PedroRenatoEnemy.list.Count; i++)
            {
                PedroRenatoEnemy.list[i].Draw(gameTime, spriteBatch);
            }

            // GERAL ----------------------------------------------------------
            for (int i = 0; i < Shoot.list.Count; i++)
            {
                Shoot.list[i].Draw(gameTime, spriteBatch);
            }

            spriteBatch.DrawString(Game1.arial24, tempoDeJogo.ToString(),
                                    new Vector2(pedroRenatoPlayer1.Window.ClientBounds.Width - Game1.arial24.MeasureString("99:99:99").X,
                                                pedroRenatoPlayer1.Window.ClientBounds.Height - Game1.arial24.MeasureString("Y").Y),
                                    Color.Yellow);


            if (pedroRenatoPlayer1.vidaPlayer1 <= 0 && pedroRenatoPlayer2.vidaPlayer2 <= 0)
            {
                spriteBatch.DrawString(Game1.arial24, "Você perdeu!", new Vector2(pedroRenatoPlayer1.Window.ClientBounds.Width / 2 - Game1.arial24.MeasureString("Você perdeu").X / 2,
                                                                                 pedroRenatoPlayer1.Window.ClientBounds.Height / 5),
                                        Color.Yellow);

                spriteBatch.DrawString(Game1.arial12,
                                        "ESC - Para Sair        R - Para Recomeçar",
                                        new Vector2(pedroRenatoPlayer1.Window.ClientBounds.Width / 2 - Game1.arial12.MeasureString("ESC - Para Sair        R - Para Recomeçar").X / 2,
                                                    pedroRenatoPlayer1.Window.ClientBounds.Height / 2 + 100),
                                        Color.Yellow);

                spriteBatch.DrawString(Game1.arial24, "Você fez" + " " + pedroRenatoPlayer1.pontos.ToString() + " " + "Pontos",
                                        new Vector2(pedroRenatoPlayer1.Window.ClientBounds.Width / 2 - Game1.arial24.MeasureString("Você fez" + pedroRenatoPlayer1.pontos.ToString() + "Pontos").X / 2,
                                                    pedroRenatoPlayer1.Window.ClientBounds.Height / 2 + Game1.arial24.MeasureString("Y").Y / 2),
                                        Color.Yellow);
            }


            spriteBatch.DrawString
                (Game1.arial24,
                //pedroRenatoPlayer1.pontosPlayer1.ToString(), 
                Shoot.list.Count.ToString(),
                new Vector2(0, pedroRenatoPlayer1.Window.ClientBounds.Height
                    - Game1.arial24.MeasureString("Y").Y), Color.Yellow);



            // ----------------------------------------------------------------------------------------- GERAL ----------------------------------------------------------------------------------------------------------- GERAL //



            // Dando o BUG -------------------------------------------------------------
            /*for (int k = 0; k < Power.list.Count; k++)
            {
                Power.list[k].Draw(gameTime, spriteBatch);
            }

            for (int j = 0; j < PedroRenatoEnemy.list.Count; j++)
            {
                PedroRenatoEnemy.list[j].Draw(gameTime, spriteBatch);
            }*/
            // Dando o BUG -------------------------------------------------------------
        }

    }
}
